import { ref } from 'vue';
import keyCodes from '@/static/json/keyCode.json'; // 请确保路径正确
export default {
    install: (app, options) => {
        const triggerKeyup = ref(null);
        app.provide('triggerKeyup', triggerKeyup);

        const handleRfidScanEvent = (event) => {
            triggerKeyup.value = null;
            triggerKeyup.value = event;
        };
        const createKeyListener = () => {
            const rfidKeyCodesList = keyCodes.map((item) => item.rfidKeyCode);
            plus.key.addEventListener(
                'keyup',
                (e) => {
                    console.log(`global keyup event, keyCode: ${e.keyCode}`);
                    if (rfidKeyCodesList.includes(e.keyCode)) {
                        handleRfidScanEvent('rfidKeyUp');
                    }
                },
                false
            );
            if (typeof plus !== 'undefined') {
            }
        };
        const deleteKeyListener = () => {
            plus.key.removeEventListener('keyup', handleRfidScanEvent);
        };

        if (typeof plus !== 'undefined') {
            deleteKeyListener();
            createKeyListener();
        }
    }
};
